package dongbai.soft.task;

import dongbai.soft.utils.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Set;

@Component
@SuppressWarnings("unchecked")
public class Update {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 将热力图列表的第一天数据弹出，新一天的0利润压入
     */
    @Scheduled(cron = "0 0 0 * * ?")
    public void UpdateHeatMap() {
        //更新利润热力图
        redisTemplate.opsForList().leftPop(Constant.PROFIT);
        redisTemplate.opsForList().rightPush(Constant.PROFIT, new BigDecimal(0));


        //更新货单日历饼图
        redisTemplate.opsForList().leftPop(Constant.CAL_PIE);
        redisTemplate.opsForList().rightPush(Constant.CAL_PIE, "0, 0, 0");

        //更新分站的折线图和柱状图
        Set<String> linekeys = redisTemplate.keys(Constant.TR_LINE + "*");
        Set<String> barkeys = redisTemplate.keys(Constant.TR_BAR + "*");
        assert linekeys != null;
        assert barkeys != null;
        for (String key : linekeys) {
            redisTemplate.opsForList().leftPop(key);
            redisTemplate.opsForList().rightPush(key, new BigDecimal(0));
        }
        for (String key : barkeys) {
            redisTemplate.opsForList().leftPop(key);
            redisTemplate.opsForList().rightPush(key, 0);
        }
    }

}
